#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>

#define trainsen 4
#define key      5
#define led      LED_BUILTIN

#define off   1

#define State(n)  digitalRead(n) 
#define Set(n)    digitalWrite(n,~off)
#define Reset(n)  digitalWrite(n,off)

char auth[] = "fdccb9c6e1d7";
char ssid[] = "z111";
char pswd[] = "zfq907631";
BlinkerText stated("tex-status");
long timer=0;

void setup() {
  pinMode(trainsen,INPUT);
  pinMode(key,INPUT);
  pinMode(led,OUTPUT);
  Serial.begin(115200);
  Serial1.begin(9600);
  Blinker.begin(auth, ssid, pswd);
}

void loop() {
  
  char doonce=1;
 
  //判断是否有人戴上帽子 无则啥也不干
  // Serial.print("是否扣上扣子：");
  // Serial.print(State(key));
  // Serial.print("  是否train到人：");
  // Serial.println(State(trainsen));
  
  if(State(trainsen)==off)
  {
    timer=millis();
    Reset(led);
    // Serial.print("  没人  ");
    stated.print("yes");
    Blinker.run();
  }
  //有则 判断是否有扣上扣子 有扣上啥也不干
  else
  {
    if(State(key)==!off)
    {
      //***以下写扣上扣子干的活***************
      timer=millis();
      Reset(led);
      pinMode(key,OUTPUT);
      Reset(key);
      pinMode(key,INPUT);
      stated.print("yes");
      Blinker.run();
      // Serial.print("  带了带了  ");
      //**********************************
    }
    else
    {
      //***以下写未扣上扣子干的活*************  1、10秒后蜂鸣器报警，亮灯，发送短信至手机
      // Serial.print("  是否扣上扣子：");
      // Serial.print(State(key));
      // Serial.println("  没带没带  ");
      // Serial.print(millis()-timer);
      // Serial.print("  ");
      while(millis()-timer>=5000 && State(key)==off &&State(trainsen)==!off)
      {
        // Serial.print("  一直没带  ");
        
        stated.print("no");
        Blinker.run();
        if(doonce)
        {
          doonce=0;
          Reset(led);
          Serial.println("AT+CMGF=1");
          Serial.println("AT+CSMP=17,167,2,25");
          Serial.println("AT+CSCS=\"UCS2\"");
          Serial.println("AT+CMGS=\"00310035003300380031003500360036003700390039\"");
          Serial.println("77ED4FE16D4B8BD5");
          //发短信
        }
      }
      
      //****************************** ****
    }
  }
}
